<?php
/**
 * 站点栏目模型
 * CategoryModel.php
 * ==============================================
 * 版权所有 2013-2014
 * ----------------------------------------------
 * 这不是一个自由软件，未经授权不许任何使用和传播。
 * ----------------------------------------------
 * 
 * @package xcs
 * @author huangdaohai <huangdaohai.2007@163.com>
 * @version V1.0
 * @access public
 */
class CategoryModel extends BaseModel
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return CategoryModel the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{category}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('site_id, module, cate_type, cate_name', 'required'),
			array('site_id, cate_type, model_id, parent_id, child_id, items, hits, isnave, sort', 'numerical', 'integerOnly'=>true),
			array('module, cate_name', 'length', 'max'=>20),
			array('parent_ids, child_ids, description', 'length', 'max'=>255),
			array('cate_image, url, letter', 'length', 'max'=>100),
			array('setting', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('cate_id, site_id, module, cate_type, model_id, parent_id, parent_ids, child_id, child_ids, cate_name, cate_image, url, items, hits, setting, isnave, letter, sort, description', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
            'modelinfo' => array(self::HAS_ONE,'CmsModel' ,'' ,'on'=>'t.model_id=modelinfo.model_id')
		);
	}
    /**
	 * 获取所有栏目列表
	 *
	 * @return array 数据数组
	 */
	public function getAllCategory(){
		$criteria = new CDbCriteria();
        
        $criteria->order = 'sort ASC ,cate_id DESC' ;//排序条件 
		$oResult = self::model()->findAll($criteria);
		$aList = array();
		if(!empty($oResult)){
			foreach ($oResult as $iKey => $aVal){
				$aList[$aVal->cate_id]['cate_id'] = $aVal->cate_id;
                $aList[$aVal->cate_id]['site_id'] = $aVal->site_id;
                $aList[$aVal->cate_id]['module'] = $aVal->module;
                $aList[$aVal->cate_id]['cate_type'] = $aVal->cate_type;
                $aList[$aVal->cate_id]['model_id'] = $aVal->model_id;
                $aList[$aVal->cate_id]['parent_ids'] = $aVal->parent_ids;
                $aList[$aVal->cate_id]['child_id'] = $aVal->child_id;
				$aList[$aVal->cate_id]['child_ids'] = $aVal->child_ids;
                $aList[$aVal->cate_id]['cate_name'] = $aVal->cate_name;
                $aList[$aVal->cate_id]['cate_image'] = $aVal->url;
                $aList[$aVal->cate_id]['url'] = $aVal->url;
                $aList[$aVal->cate_id]['items'] = $aVal->items;
                $aList[$aVal->cate_id]['hits'] = $aVal->hits;
                $aList[$aVal->cate_id]['setting'] = $aVal->setting;
                $aList[$aVal->cate_id]['isnave'] = $aVal->isnave;
                $aList[$aVal->cate_id]['letter'] = $aVal->letter;
                $aList[$aVal->cate_id]['sort'] = $aVal->sort;
                $aList[$aVal->cate_id]['description'] = $aVal->description;
			}
		}
		return $aList;
	}
    
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'cate_id' => 'cateid',
			'site_id' => '站点ID',
			'module' => '模块',
			'cate_type' => '栏目类型',
			'model_id' => '模型ID',
			'parent_id' => '上线栏目',
			'parent_ids' => '所有父级ID串',
			'child_id' => '子级',
			'child_ids' => '子级ID串',
			'cate_name' => '栏目名称',
			'cate_image' => '栏目图标',
			'url' => '栏目地址',
			'items' => '数据量',
			'hits' => '点击数',
			'setting' => '设置',
			'isnave' => '是否在导航显示',
			'letter' => '栏目拼音',
			'sort' => '排序',
			'description' => '描述',
            'linkurl'=>'访问地址',
            'default_style' => '风格选择',
            'category_template' => '栏目首页模板',
            'list_template' => '栏目列表模板',
            'show_template' => '栏目详细模板',
            'seo_title' =>'栏目标题',
            'seo_keyword' =>'栏目关键词',
            'seo_description' => '栏目描述'
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('cate_id',$this->cate_id);
		$criteria->compare('site_id',$this->site_id);
		$criteria->compare('module',$this->module,true);
		$criteria->compare('cate_type',$this->cate_type);
		$criteria->compare('model_id',$this->model_id);
		$criteria->compare('parent_id',$this->parent_id);
		$criteria->compare('parent_ids',$this->parent_ids,true);
		$criteria->compare('child_id',$this->child_id);
		$criteria->compare('child_ids',$this->child_ids,true);
		$criteria->compare('cate_name',$this->cate_name,true);
		$criteria->compare('cate_image',$this->cate_image,true);
		$criteria->compare('url',$this->url,true);
		$criteria->compare('items',$this->items);
		$criteria->compare('hits',$this->hits);
		$criteria->compare('setting',$this->setting,true);
		$criteria->compare('isnave',$this->isnave);
		$criteria->compare('letter',$this->letter,true);
		$criteria->compare('sort',$this->sort);
		$criteria->compare('description',$this->description,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}